<?php

namespace App\Services\Rate;

use App\Models\Company\CompanyConfig;
use App\Models\Store;
use App\Models\StoreAllPayWayList;
use App\Models\StorePayWay;
use App\Models\StoreWaysDesc;
use App\Models\User;
use App\Models\UserRate;
use App\Repository\VbillRateSaveLogRepo;
use Illuminate\Support\Facades\DB;

class UserRateService
{

    protected $sqlUserRate = "case store_ways_desc.ways_source when \"weixin\" then 1 when \"alipay\" then 1 when \"unionpay\" then 1 when \"Bestpay\" then 2 when \"applet\" then 3 end";


    protected $sqlStorePayWay = "case ways_source when \"weixin\" then 1 when \"alipay\" then 1 when \"unionpay\" then 1 when \"Bestpay\" then 2 when \"applet\" then 3 end";

    public function costRateList(int $own_user_id, int $user_id, array $ways_source): array
    {
        $company = StoreAllPayWayList::query()->get()->toArray();
        $companyName = array_column($company, "company");
        $company = array_combine($companyName, $company);
        //先获取自己名下的所有支付通道
        $list = UserRate::query()
            ->select([DB::raw("(" . $this->sqlUserRate . ") as type"),
                "user_rates.company", "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c",
                "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top", "user_rates.server_rate",
                "user_rates.yu_rate", "user_rates.era_rate"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $own_user_id)
            ->whereIn("user_rates.company", $companyName)
            ->whereIn("store_ways_desc.ways_source", $ways_source)
//            ->whereIn("ways_type", $ways_types)
            ->groupBy([
                "user_rates.company", "user_rates.rate", "user_rates.rate_a",
                "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f",
                "user_rates.rate_f_top",
                "user_rates.server_rate", "user_rates.yu_rate", "user_rates.era_rate"
            ])
            ->get()->toArray();
        //查看userId的支付通道
        $userRateList = UserRate::query()
            ->select([
                "user_rates.company", "user_rates.rate",
                "user_rates.rate_a", "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f",
                "user_rates.rate_f_top",
                "user_rates.server_rate",
                "user_rates.yu_rate",
                "user_rates.era_rate"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $user_id)
            ->whereIn("user_rates.company", $companyName)
            ->whereIn("store_ways_desc.ways_source", $ways_source)
//            ->whereIn("ways_type", $ways_types)
            ->groupBy([
                "user_rates.company",
                "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c",
                "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top",
                "user_rates.server_rate", "user_rates.yu_rate", "user_rates.era_rate"
            ])
            ->get()->toArray();
        $userRateList = array_combine(array_column($userRateList, "company"), $userRateList);
        //查看公司名称
        $data = [];
        foreach ($list as $v) {
            //渲染公司名称
            $v["company_name"] = $company[$v["company"]]["company_desc"] ?? "";
            $tmp = [
                "is_open" => 0,
                "type" => $v["type"],
                "rate" => $v["rate"],
                "server_rate" => 0,
                "yu_rate" => 0,
                "era_rate" => 0,
                "rate_a" => $v["rate_a"],
                "rate_c" => $v["rate_c"],
                "rate_e" => $v["rate_e"],
                "rate_f" => $v["rate_f"],
                "rate_f_top" => $v["rate_f_top"],
                "company" => $v["company"],
                "company_name" => $v["company_name"],
            ];
            $userRate = $userRateList[$v["company"]] ?? [];
            if (!empty($userRate)) {
                $tmp = [
                    "is_open" => 1,
                    "type" => $v["type"],
                    "rate" => $userRate["rate"],
                    "server_rate" => $userRate["server_rate"],
                    "yu_rate" => $userRate["yu_rate"],
                    "era_rate" => $userRate["era_rate"],
                    "rate_a" => $userRate["rate_a"],
                    "rate_c" => $userRate["rate_c"],
                    "rate_e" => $userRate["rate_e"],
                    "rate_f" => $userRate["rate_f"],
                    "rate_f_top" => $userRate["rate_f_top"],
                    "company" => $v["company"],
                    "company_name" => $v["company_name"],
                ];
            }
            //渲染
            $data[] = $tmp;
            unset($tmp);
        }
        return $data;
    }


    public function defaultRateList(int $user_id, array $ways_source): array
    {
//        $data[$k]['store_all_rate'] = $has["store_all_rate"];
//        $data[$k]['store_all_rate_a'] = $has["store_all_rate_a"];
//        $data[$k]['store_all_rate_b'] = $has["store_all_rate_b"];
//        $data[$k]['store_all_rate_b_top'] = $has["store_all_rate_b_top"];
//        $data[$k]['store_all_rate_c'] = $has["store_all_rate_c"];
//        $data[$k]['store_all_rate_d'] = $has["store_all_rate_d"];
//        $data[$k]['store_all_rate_d_top'] = $has["store_all_rate_d_top"];
//        $data[$k]['store_all_rate_e'] = $has["store_all_rate_e"];
//        $data[$k]['store_all_rate_f'] = $has["store_all_rate_f"];
//        $data[$k]['store_all_rate_f_top'] = $has["store_all_rate_f_top"];

        $store_ways_desc = StoreWaysDesc::query()
            ->select([DB::raw("(" . $this->sqlUserRate . ") as type"), "company", "company_name", "store_all_rate", "store_all_rate_a",
                "store_all_rate_c", "store_all_rate_e", "store_all_rate_f", "store_all_rate_f_top"])
            ->groupBy(["company", "company_name", "store_all_rate", "store_all_rate_a", "store_all_rate_c", "store_all_rate_e", "store_all_rate_f", "store_all_rate_f_top"])
            ->whereIn("ways_source", $ways_source)
            ->get()
            ->toArray();
        //支付type
        $companys = array_column($store_ways_desc, "company");
        //        $ways_types = StoreWaysDesc::query()
//            ->whereIn("ways_source", $ways_source)
//            ->pluck("ways_type")
//            ->toArray();
        //检测是否开通
        $userRateArray = UserRate::query()
            ->select(["company", "store_all_rate", "store_all_rate_a", "store_all_rate_c", "store_all_rate_e", "store_all_rate_f", "store_all_rate_f_top"])
            ->where("user_id", $user_id)
            ->whereIn("company", $companys)
            ->whereIn("ways_source", $ways_source)
//            ->whereIn("ways_type", $ways_types)
            ->groupBy(["company", "store_all_rate", "store_all_rate_a", "store_all_rate_c", "store_all_rate_e", "store_all_rate_f",
                "store_all_rate_f_top"])
            ->get()->toArray();
        $userRateArray = array_combine(array_column($userRateArray, "company"), $userRateArray);
        //获取成本费率
        $data = [];
        foreach ($store_ways_desc as $v) {
            if (!isset($userRateArray[$v["company"]])) {
                continue;
            }
            //
            $companyRate = $userRateArray[$v["company"]] ?? [];
            $tmp = [
                "type" => $v["type"],
                "store_all_rate" => $companyRate["store_all_rate"] ?? 0,
                "store_all_rate_a" => $companyRate["store_all_rate_a"] ?? 0,
                "store_all_rate_c" => $companyRate["store_all_rate_c"] ?? 0,
                "store_all_rate_e" => $companyRate["store_all_rate_e"] ?? 0,
                "store_all_rate_f" => $companyRate["store_all_rate_f"] ?? 0,
                "store_all_rate_f_top" => $companyRate["store_all_rate_f_top"] ?? 0,
                "company" => $companyRate["company"] ?? "",
                "company_name" => $v["company_name"],
            ];
            $data[] = $tmp;
        }

        return $data;
    }


    public function payRateList(Store $store, array $ways_source): array
    {
        $company = StoreAllPayWayList::query()->get()->toArray();
        $companyName = array_column($company, "company");
        $company = array_combine($companyName, $company);
//        $storeWaysDesc = StoreWaysDesc::query()
//            ->select([DB::raw("(" . $this->sql . ") as type"), "company", "settlement_type", "company_name", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top"])
//            ->groupBy(["company", "settlement_type", "company_name", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top"])
//            ->whereIn("ways_source", $ways_source)
//            ->get()
//            ->toArray();
//        $storeWaysDesc = array_combine(array_column($storeWaysDesc, "company"), $storeWaysDesc);
        //查看代理商的通道
//        $companyName=["vbill"];

//        select  user_rates.*, store_ways_desc.ways_source from user_rates left join store_ways_desc on user_rates.ways_type=store_ways_desc.ways_type where user_rates.ways_source="";

        $agentId = $store->user_id;
        $list = UserRate::query()
            ->select([DB::raw("(" . $this->sqlUserRate . ") as type"),
                "user_rates.company",
                "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top", "user_rates.server_rate", "user_rates.yu_rate", "user_rates.era_rate",
                "user_rates.store_all_rate", "user_rates.store_all_rate_a", "user_rates.store_all_rate_c", "user_rates.store_all_rate_e", "user_rates.store_all_rate_f", "user_rates.store_all_rate_f_top"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $agentId)
            ->whereIn("user_rates.company", $companyName)
            ->where("user_rates.status", 1)
            ->whereIn("store_ways_desc.ways_source", $ways_source)
//            ->whereIn("ways_type", $ways_types)
            ->groupBy(["user_rates.company",
                "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top", "user_rates.server_rate", "user_rates.yu_rate", "user_rates.era_rate",
                "user_rates.store_all_rate", "user_rates.store_all_rate_a", "user_rates.store_all_rate_c", "user_rates.store_all_rate_e", "user_rates.store_all_rate_f", "user_rates.store_all_rate_f_top"
            ])
            ->get()->toArray();
        //查看自己的通道
        $storePayWays = StorePayWay::query()
            ->select([DB::raw("(" . $this->sqlStorePayWay . ") as type"), "company", "ways_type", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source", "settlement_type"])
            ->where('store_id', $store->store_id)
            ->whereIn('ways_source', $ways_source)
            ->groupBy(["company", "ways_type", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source", "settlement_type"])
            ->get()->toArray();
        $ways_source_tmp = $storeRateArray = [];
        foreach ($storePayWays as $k => $v) {
            $ways_source_tmp[$v["company"]][] = [
                "ways_source" => $v["ways_source"],
                "ways_type" => $v["ways_type"],
                "status" => $v["status"],
                "status_desc" => $v["status_desc"]
            ];
            $v["action"] = $ways_source_tmp[$v["company"]];
            $storeRateArray[$v["company"]] = $v;
        }
        $data = [];
        //循环设置数据
        foreach ($list as $value) {
            $value["company_name"] = $company[$value["company"]]["company_desc"] ?? "";
            $tmp = [
                "company" => $value["company"],
                "settlement_type" => "",
                "type" => $value['type'],
                "company_name" => $value['company_name'],
                "status" => 0,
                "status_desc" => "未开通",
                "rate" => $value["store_all_rate"] ?? 0,//$value["store_all_rate"] ?? 0,
                "rate_a" => $value["store_all_rate_a"] ?? 0, //store_all_rate_a
                "rate_c" => $value["store_all_rate_c"] ?? 0, //store_all_rate_c
                "rate_e" => $value["store_all_rate_e"] ?? 0, //store_all_rate_e
                "rate_f" => $value["store_all_rate_f"] ?? 0, //store_all_rate_f
                "rate_f_top" => $value["store_all_rate_f_top"] ?? 0, //store_all_rate_f_top
                "action" => null
            ];
            $storeRate = $storeRateArray[$value["company"]] ?? [];
            if (!empty($storeRate)) {
                //显示店铺费率
                $status_desc = $storeRate["status"] == 1 ? "已开通" : ($storeRate["status"] == 0 ? "未开通" : "审核中");
                $tmp = [
                    "company" => $value["company"],
                    "settlement_type" => $storeRate["settlement_type"],
                    "type" => $value['type'],
                    "company_name" => $value['company_name'],
                    "status" => $storeRate["status"],
                    "status_desc" => $status_desc,
                    "rate" => $storeRate["rate"],
                    "rate_a" => $storeRate["rate_a"],
                    "rate_c" => $storeRate["rate_c"],
                    "rate_e" => $storeRate["rate_e"],
                    "rate_f" => $storeRate["rate_f"],
                    "rate_f_top" => $storeRate["rate_f_top"],
                    "action" => $storeRate["action"],
                ];
            }
            $data[] = $tmp;
        }
        return $data;


    }


    public function payRateListBak(Store $store, array $ways_source)
    {
        $store_ways_desc = StoreWaysDesc::query()
            ->select([DB::raw("(" . $this->sql . ") as type"), "company", "settlement_type", "company_name", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top"])
            ->groupBy(["company", "settlement_type", "company_name", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top"])
            ->whereIn("ways_source", $ways_source)
            ->get()
            ->toArray();
        $companys = array_column($store_ways_desc, "company");
        //获取代理商的费率
        $userRateArray = UserRate::query()->whereIn('company', $companys)
            ->where('user_id', $store->user_id)
            ->select([DB::raw("(" . $this->sql . ") as type"), "company", "store_all_rate", "store_all_rate_a",
                "store_all_rate_c", "store_all_rate_e", "store_all_rate_f", "store_all_rate_f_top"])
            ->groupBy(["company", "store_all_rate", "store_all_rate_a", "store_all_rate_c", "store_all_rate_e", "store_all_rate_f", "store_all_rate_f_top"])
            ->get()->toArray();
        $userRateArray = array_combine(array_column($userRateArray, "company"), $userRateArray);
        //获取店铺费率
        $storeRateArrayWaysSource = StorePayWay::query()
            ->select([DB::raw("(" . $this->sql . ") as type"), "company", "ways_type", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source"])
            ->where('store_id', $store->store_id)
            ->whereIn('ways_source', $ways_source)
            ->groupBy(["company", "ways_type", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source"])
            ->get()->toArray();
        $storeRateArray = [];
        //进行数据合并和整理
        $ways_source_tmp = [];
        foreach ($storeRateArrayWaysSource as $k => $v) {
            $ways_source_tmp[$v["company"]][] = [
                "ways_source" => $v["ways_source"],
                "ways_type" => $v["ways_type"],
                "status" => $v["status"],
                "status_desc" => $v["status_desc"]
            ];
            $v["action"] = $ways_source_tmp[$v["company"]];
            $storeRateArray[$v["company"]] = $v;
        }
//        $storeRateArray = array_combine(array_column($storeRateArray, "company"), $storeRateArray);
        $data = [];
        //循环设置数据
        foreach ($store_ways_desc as $value) {
            //代理商没有不显示
            $userRate = $userRateArray[$value['company']] ?? [];
            if (!$userRate) {
                continue;
            }
            //显示店铺费率
            $storeRate = $storeRateArray[$value["company"]] ?? [];
            if (!empty($storeRate)) {
                //显示店铺费率
                $status_desc = $storeRate["status"] == 1 ? "已开通" : ($storeRate["status"] == 0 ? "未开通" : "审核中");
                $tmp = [
                    "company" => $value["company"],
                    "settlement_type" => $value["settlement_type"],
                    "type" => $value['type'],
                    "company_name" => $value['company_name'],
                    "status" => $storeRate["status"],
                    "status_desc" => $status_desc,
                    "rate" => $storeRate["rate"],
                    "rate_a" => $storeRate["rate_a"],
                    "rate_c" => $storeRate["rate_c"],
                    "rate_e" => $storeRate["rate_e"],
                    "rate_f" => $storeRate["rate_f"],
                    "rate_f_top" => $storeRate["rate_f_top"],
                    "action" => $storeRate["action"],
                ];
            } else {
                //有代理商费率覆盖
                $tmp = [
                    "company" => $value["company"],
                    "settlement_type" => $value["settlement_type"],
                    "type" => $value['type'],
                    "company_name" => $value['company_name'],
                    "status" => 0,
                    "status_desc" => "未开通",
                    "rate" => $userRate["store_all_rate"],
                    "rate_a" => $userRate["store_all_rate_a"],
                    "rate_c" => $userRate["store_all_rate_c"],
                    "rate_e" => $userRate["store_all_rate_e"],
                    "rate_f" => $userRate["store_all_rate_f"],
                    "rate_f_top" => $userRate["store_all_rate_f_top"],
                    "action" => null
                ];
            }
            $data[] = $tmp;
        }
        return $data;
    }

    //店铺费率
    public function payWaylists(Store $store, array $where): array
    {
        //组装数据,并且去重复

        //在这里获取通道名称
//        select user_rates . company,store_ways_desc . company_name from user_rates left join store_ways_desc on user_rates . company = store_ways_desc . company where user_rates . user_id = 1 and store_ways_desc . company_name is not null  group by user_rates . company,store_ways_desc . company_name
        $companyAll = UserRate::query()->select(
            [
                "user_rates.company",
                "store_ways_desc.company_name as company_name"
            ])
            ->leftJoin("store_ways_desc", "user_rates.company", "=", "store_ways_desc.company")
            ->where("user_id", $store->user_id)
            ->where(function ($query) use ($where) {
                if (!empty($where["company"])) {
                    $query->where("user_rates.company", $where["company"]);
                }
            })
            ->where("user_rates.status", 1)
            ->whereNotNull("store_ways_desc.company_name")
            ->groupBy(["user_rates.company", "store_ways_desc.company_name"])
            ->get()->toArray();
        $companyArray = array_column($companyAll, "company");
//        $company = array_combine($companyName, $companyAll);
        //查询company下的成本费率
        $companyUserRate = UserRate::query()
            ->select([
                "user_rates.company",
                "store_ways_desc.ways_desc",
                "store_ways_desc.ways_source",
                "user_rates.ways_type",
                "user_rates.status",
                "user_rates.settlement_type", "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top",
                "user_rates.server_rate", "user_rates.yu_rate", "user_rates.era_rate",
                "user_rates.store_all_rate", "user_rates.store_all_rate_a", "user_rates.store_all_rate_c",
                "user_rates.store_all_rate_e", "user_rates.store_all_rate_f", "user_rates.store_all_rate_f_top"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $store->user_id)
            ->whereIn("user_rates.company", $companyArray)
            ->where("user_rates.status", 1)
            ->get()->toArray();
        $tmpUserRate = $tmpUserRateWaySource = $tmpUserRateWaySourceFirst = [];
        foreach ($companyUserRate as $v) {
            $v["status_desc"] = "未开通";
            $tmpUserRateWaySourceFirst[$v["company"]] = $v;
            $tmpUserRate[$v["company"]][] = $v;
            $tmpUserRateWaySource[$v["company"]][$v["ways_source"]] = $v;
        }
        //店铺通道处理
        $storePayWays = StorePayWay::query()
            ->select(["store_id", "company", "ways_type",
                "ways_desc", "pay_amount_e",
                "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source", "settlement_type"])
            ->where('store_id', $store->store_id)
            ->whereIn('company', $companyArray)
//            ->groupBy(["company", "ways_type", "rate", "rate_a", "rate_c", "rate_e", "rate_f", "rate_f_top", "status", "status_desc", "ways_source", "settlement_type"])
            ->get()->toArray();
        $tmpStorePayWays = $tmpStorePayWaysWaySource = $tmpStorePayWaysWaySourceFirst = [];
        foreach ($storePayWays as $v) {
            $tmpStorePayWays[$v["company"]][] = $v;
            $tmpStorePayWaysWaySourceFirst[$v["company"]] = $v;
            $tmpStorePayWaysWaySource[$v["company"]][$v["ways_source"]] = $v;
        }
//        dd($tmpStorePayWays);

        $companyConfig = CompanyConfig::query()->where("store_id", $store->store_id)->whereIn("company", $companyArray)->get()->toArray();
        $companyConfig = array_combine(array_column($companyConfig, "company"), $companyConfig);
        //在这里循环检测并且处理费率值
        foreach ($companyAll as $k => $v) {
            //检测是否开通
            $v["is_open"] = 0;
            $v["type"] = $tmpUserRate[$v["company"]] ?? [];
            $v["receive_model"] = 0;
            $v["ways_type"] = $tmpUserRateWaySourceFirst[$v["company"]]["ways_type"] ?? "";
            //在这里处理费率
            //Bestpay "weixin", "alipay", "unionpay"
            $tmpRate = [
                "rate_a" => $tmpUserRateWaySource[$v["company"]]["unionpay"]["rate_a"] ?? 0,
                "rate_c" => $tmpUserRateWaySource[$v["company"]]["unionpay"]["rate_c"] ?? 0,
                "rate_e" => $tmpUserRateWaySource[$v["company"]]["unionpay"]["rate_e"] ?? 0,
                "rate_f" => $tmpUserRateWaySource[$v["company"]]["unionpay"]["rate_f"] ?? 0,
                "rate_f_top" => $tmpUserRateWaySource[$v["company"]]["unionpay"]["rate_f_top"] ?? 0,
                "rate_bestpay" => $tmpUserRateWaySource[$v["company"]]["Bestpay"]["rate"] ?? 0, //翼支付
                "rate_wx" => $tmpUserRateWaySource[$v["company"]]["weixin"]["rate"] ?? 0, //翼支付
                "rate_alipay" => $tmpUserRateWaySource[$v["company"]]["alipay"]["rate"] ?? 0, //翼支付

                "server_rate" => $tmpUserRateWaySourceFirst[$v["company"]]["server_rate"] ?? 0, //翼支付
                "yu_rate" => $tmpUserRateWaySourceFirst[$v["company"]]["yu_rate"] ?? 0, //翼支付
                "era_rate" => $tmpUserRateWaySourceFirst[$v["company"]]["era_rate"] ?? 0, //翼支付
            ];

            if (isset($tmpStorePayWays[$v["company"]])) {
                $v["is_open"] = 1;
                $v["type"] = $tmpStorePayWays[$v["company"]];
                //新增扣费模式
                $v["receive_model"] = $companyConfig[$v["company"]]["model"] ?? 0;
                $v["ways_type"] = $tmpStorePayWaysWaySourceFirst[$v["company"]]["ways_type"] ?? "";
                $tmpRate = [
                    "rate_a" => $tmpStorePayWaysWaySource[$v["company"]]["unionpay"]["rate_a"] ?? 0,
                    "rate_c" => $tmpStorePayWaysWaySource[$v["company"]]["unionpay"]["rate_c"] ?? 0,
                    "rate_e" => $tmpStorePayWaysWaySource[$v["company"]]["unionpay"]["rate_e"] ?? 0,
                    "rate_f" => $tmpStorePayWaysWaySource[$v["company"]]["unionpay"]["rate_f"] ?? 0,
                    "rate_f_top" => $tmpStorePayWaysWaySource[$v["company"]]["unionpay"]["rate_f_top"] ?? 0,
                    "rate_bestpay" => $tmpStorePayWaysWaySource[$v["company"]]["Bestpay"]["rate"] ?? 0, //翼支付
                    "rate_wx" => $tmpStorePayWaysWaySource[$v["company"]]["weixin"]["rate"] ?? 0, //翼支付
                    "rate_alipay" => $tmpStorePayWaysWaySource[$v["company"]]["alipay"]["rate"] ?? 0, //翼支付

                    "server_rate" => $companyConfig[$v["company"]]["configure_channel_fee"] ?? 0,
                    "yu_rate" => $companyConfig[$v["company"]]["configure_channel_fee"] ?? 0,
                    "era_rate" => $companyConfig[$v["company"]]["configure_channel_fee"] ?? 0,
                ];
            }
            $v["rate"] = $tmpRate;
            /**
             * 获取待生效数据
             */
            $v['rate_pending_effect'] = [
                'status'=>0,
                'rate'=>[
                    'rate_wx'=>'',
                    'rate_alipay'=>'',
                    'rate_a'=>'',
                    'rate_c'=>'',
                ]
            ];

            /**
             * 根据门店ID获取待生效数据
             */
            if($v["company"] == 'vbill')
            {
                $queryWhere = [
                    'store_id' => $store->store_id,
                    'status'=>0,
                ];
                $log = VbillRateSaveLogRepo::getInstance()->getQueryData($queryWhere);
                if ($log->isNotEmpty())
                {
                    $logData = $log->toArray();
                    $v['rate_pending_effect']['status'] = $logData[0]['status'] == 1 ? 0 :1;
                    $v['rate_pending_effect']['rate']['rate_wx'] = $logData[0]['rate_wx'];
                    $v['rate_pending_effect']['rate']['rate_alipay'] = $logData[0]['rate_alipay'];
                    $v['rate_pending_effect']['rate']['rate_a'] = $logData[0]['rate_a'];
                    $v['rate_pending_effect']['rate']['rate_c'] = $logData[0]['rate_c'];
                }
            }
            $companyAll[$k] = $v;
        }
        return $companyAll;
    }

    //成本费率
    public function userRateList(int $login_user_id, int $agent_id, array $where): array
    {
        $is_top = 0;
        if ($login_user_id == 1) {
            $is_top = 1;
        }
        //代理商的费率列表要从上级过来继承
        $agent = User::query()->where("id", $agent_id)->first();
        if (empty($agent)) {
            throw new \Exception("账号不存在");
        }
        $last_agent_id = $agent->pid;
        if (empty($last_agent_id)) {
            $last_agent_id = $agent_id;
        }
        $companyAll = UserRate::query()->select(
            [
                "user_rates.company",
                "store_ways_desc.company_name"
            ])
            ->leftJoin("store_ways_desc", "user_rates.company", "=", "store_ways_desc.company")
            ->where("user_id", $last_agent_id)
            ->where(function ($query) use ($where) {
                if (!empty($where["company"])) {
                    $query->where("user_rates.company", $where["company"]);
                }
            })
            ->where("user_rates.status", 1)
            ->whereNotNull("store_ways_desc.company_name")
            ->groupBy(["user_rates.company", "store_ways_desc.company_name"])
            ->get()->toArray();

        $companyArray = array_column($companyAll, "company");
        //先获取自己名下的所有支付通道
        $lastLevelRateArray = UserRate::query()
            ->select([
                "user_rates.company",
                "user_rates.ways_type",
                "store_ways_desc.ways_desc",
                "store_ways_desc.ways_source",
                "user_rates.rate", "user_rates.rate_a", "user_rates.rate_c",
                "user_rates.rate_e", "user_rates.rate_f", "user_rates.rate_f_top", "user_rates.server_rate",
                "user_rates.yu_rate", "user_rates.era_rate"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $last_agent_id)
            ->whereIn("user_rates.company", $companyArray)
            ->where("user_rates.status", 1)
            ->get()->toArray();
        $tmplastLevelUserRate = $tmplastLevelUserRateWaysSource = $tmplastLevelUserRateWaysSourceFirst = [];
        foreach ($lastLevelRateArray as $v) {
            $tmplastLevelUserRateWaysSourceFirst[$v["company"]] = $v;
            $tmplastLevelUserRate[$v["company"]][] = $v;
            $tmplastLevelUserRateWaysSource[$v["company"]][$v["ways_source"]] = $v;

        }
        //查看userId的支付通道
        $userRateList = UserRate::query()
            ->select([
                "user_rates.company",
                "user_rates.ways_type",
                "store_ways_desc.ways_desc",
                "store_ways_desc.ways_source",
                "user_rates.rate",
                "user_rates.rate_a", "user_rates.rate_c", "user_rates.rate_e", "user_rates.rate_f",
                "user_rates.rate_f_top",
                "user_rates.server_rate",
                "user_rates.yu_rate",
                "user_rates.era_rate"
            ])
            ->leftJoin("store_ways_desc", "user_rates.ways_type", "=", "store_ways_desc.ways_type")
            ->where("user_rates.user_id", $agent_id)
            ->whereIn("user_rates.company", $companyArray)
            ->where("user_rates.status", 1)
            //            ->whereIn("ways_type", $ways_types)
            ->get()->toArray();
        $tmpUserRateList = $tmpUserRateListWaysSource = $tmpUserRateListWaysSourceFirst = [];
        foreach ($userRateList as $v) {
            $tmpUserRateList[$v["company"]][] = $v;
            $tmpUserRateListWaysSourceFirst[$v["company"]] = $v;
            $tmpUserRateListWaysSource[$v["company"]][$v["ways_source"]] = $v;
        }
        //在这里处理数据
        foreach ($companyAll as $k => $v) {
            //检测是否开通
            $v["is_open"] = 0;
            $v["type"] = $tmplastLevelUserRate[$v["company"]] ?? [];
            $v["is_top"] = $is_top;
            $tmpRate = [
                "rate_a" => $tmplastLevelUserRateWaysSource[$v["company"]]["unionpay"]["rate_a"] ?? 0,
                "rate_c" => $tmplastLevelUserRateWaysSource[$v["company"]]["unionpay"]["rate_c"] ?? 0,
                "rate_e" => $tmplastLevelUserRateWaysSource[$v["company"]]["unionpay"]["rate_e"] ?? 0,
                "rate_f" => $tmplastLevelUserRateWaysSource[$v["company"]]["unionpay"]["rate_f"] ?? 0,
                "rate_f_top" => $tmplastLevelUserRateWaysSource[$v["company"]]["unionpay"]["rate_f_top"] ?? 0,
                "rate_bestpay" => $tmplastLevelUserRateWaysSource[$v["company"]]["Bestpay"]["rate"] ?? 0, //翼支付
                "rate_wx" => $tmplastLevelUserRateWaysSource[$v["company"]]["weixin"]["rate"] ?? 0, //翼支付
                "rate_alipay" => $tmplastLevelUserRateWaysSource[$v["company"]]["alipay"]["rate"] ?? 0, //翼支付

                "server_rate" => $tmplastLevelUserRateWaysSourceFirst[$v["company"]]["server_rate"] ?? 0, //翼支付
                "yu_rate" => $tmplastLevelUserRateWaysSourceFirst[$v["company"]]["yu_rate"] ?? 0, //翼支付
                "era_rate" => $tmplastLevelUserRateWaysSourceFirst[$v["company"]]["era_rate"] ?? 0, //翼支付
            ];
            if (isset($tmpUserRateList[$v["company"]])) {
                $v["is_open"] = 1;
                $v["type"] = $tmpUserRateList[$v["company"]];
                $tmpRate = [
                    "rate_a" => $tmpUserRateListWaysSource[$v["company"]]["unionpay"]["rate_a"] ?? 0,
                    "rate_c" => $tmpUserRateListWaysSource[$v["company"]]["unionpay"]["rate_c"] ?? 0,
                    "rate_e" => $tmpUserRateListWaysSource[$v["company"]]["unionpay"]["rate_e"] ?? 0,
                    "rate_f" => $tmpUserRateListWaysSource[$v["company"]]["unionpay"]["rate_f"] ?? 0,
                    "rate_f_top" => $tmpUserRateListWaysSource[$v["company"]]["unionpay"]["rate_f_top"] ?? 0,
                    "rate_bestpay" => $tmpUserRateListWaysSource[$v["company"]]["Bestpay"]["rate"] ?? 0, //翼支付
                    "rate_wx" => $tmpUserRateListWaysSource[$v["company"]]["weixin"]["rate"] ?? 0, //翼支付
                    "rate_alipay" => $tmpUserRateListWaysSource[$v["company"]]["alipay"]["rate"] ?? 0, //翼支付

                    "server_rate" => $tmpUserRateListWaysSourceFirst[$v["company"]]["server_rate"],
                    "yu_rate" => $tmpUserRateListWaysSourceFirst[$v["company"]]["yu_rate"],
                    "era_rate" => $tmpUserRateListWaysSourceFirst[$v["company"]]["era_rate"],
                ];
            }
            $v["rate"] = $tmpRate;
            $companyAll[$k] = $v;
        }
        return $companyAll;
    }
}